package engine;

/**
 * this class checks and transfers for svg value
 * 
 */
public class TranslateSvguUnits
{
    private String unitString;
    private double value;
    
    public double getValue(String string)
    {
        this.unitString = string;
        // check for pixels
        if (string.contains("px"))
        {
            int c = this.unitString.indexOf("p");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 1;
            return this.value;
        }
        // check for milimeters
        else if (string.contains("mm"))
        {
            int c = this.unitString.indexOf("m");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 3.779527559;
            return this.value;
        }
        // check for centimeters
        else if (string.contains("cm"))
        {
            int c = this.unitString.indexOf("c");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 37.79527559;
            return this.value;
        }
        // check for value in inches
        else if (string.contains("in"))
        {
            int c = this.unitString.indexOf("in");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 3.779527559 * 25.4;
            return this.value;
        }
        // check for value in pt
        else if (string.contains("pt"))
        {
            int c = this.unitString.indexOf("p");
            String sg = new String(this.unitString.substring(0, c));
            this.value = (parseDouble(sg) * 3) / 4;
            return this.value;
        }
        // check for value in pc
        else if (string.contains("pc"))
        {
            
            int c = this.unitString.indexOf("p");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg);
            return this.value;
            
        }
        // check for value in em
        else if (string.contains("em"))
        {
            int c = this.unitString.indexOf("e");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 16;
            return this.value;
        }
        // check for value in em
        else if (string.contains("ex"))
        {
            int c = this.unitString.indexOf("ex");
            String sg = new String(this.unitString.substring(0, c));
            this.value = parseDouble(sg) * 6;
            return this.value;
            
        }
        else
        {
            this.value = parseDouble(unitString);
            return this.value;
            
        }
    }
    
    /**
     * extended version of parseDouble handle errors
     * 
     * @param string
     * @return double
     */
    public double parseDouble(String string)
    {
        try
        {
            double kk = Double.parseDouble(string);
            return kk;
        }
        catch (Exception e)
        {
            System.out.println("Error...in SVG file units Exiting -"
                    + string.toString());
            System.exit(0);
            
        }
        return (Double) null;
    }
}
